package pe.gob.foncodes.ssa.bs.seguridad.domain;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import pe.gob.foncodes.ssa.bs.common.domain.EntidadBase;
import pe.gob.foncodes.ssa.bs.common.util.Constante;

/**
 *
 * @author Gcarlos
 */
@Entity
@Table(name = "CONEXION", catalog = "", schema = "SSA")
@NamedQueries({
    @NamedQuery(name = "Conexion.findAll", query = "SELECT c FROM Conexion c"),
    @NamedQuery(name = "Conexion.findByTxtServername", query = "SELECT c FROM Conexion c WHERE c.txtServername = :txtServername"),
    @NamedQuery(name = "Conexion.findByTxtDatabase", query = "SELECT c FROM Conexion c WHERE UPPER(c.txtDatabase) LIKE ?1 ORDER BY c.id"),
    @NamedQuery(name = "Conexion.findByTxtConexion", query = "SELECT c FROM Conexion c WHERE c.txtConexion LIKE ?1 ORDER BY c.id"),
    @NamedQuery(name = "Conexion.findByTxtPerfil", query = "SELECT u.codUsuario, CONCAT(u.txtApellidoPaterno, ' ', u.txtApellidoMaterno, ' ', u.txtNombres),"
       + " m.txtModulo, p.txtPerfil, CONCAT(to_char(c.id),' - ',c.txtConexion),"
       + " p.flgBase, up.flgConsulta, p.flgActivo"
       + " FROM Usuario u"
       + " INNER JOIN u.usuarioPerfilList up"
       + " INNER JOIN up.nidPerfil p"
       + " INNER JOIN p.nidModulo m"
       + " INNER JOIN p.nidConexion c"
       + " WHERE (m.id = ?1 OR ?1 IS NULL)"
       + " AND (u.codUsuario LIKE ?2 OR ?2 IS NULL)"
       + " AND (UPPER(CONCAT(u.txtApellidoPaterno, ' ', u.txtApellidoMaterno, ' ', u.txtNombres)) LIKE ?3 OR ?3 IS NULL)"
       + " AND u.flgActivo = '" + Constante.ESTADO_ACTIVO + "'"
       + " AND up.flgActivo = '" + Constante.ESTADO_ACTIVO + "'"
//       + " AND p.flgActivo = '" + Constante.ESTADO_ACTIVO + "'"
       + " AND m.flgActivo = '" + Constante.ESTADO_ACTIVO + "'"
       + " ORDER BY u.codUsuario, m.txtModulo, p.txtPerfil"),
    @NamedQuery(name = "Conexion.findByReporte", query = "SELECT u.codUsuario"
                                                        + " , CONCAT(u.txtApellidoPaterno, ' ', u.txtApellidoMaterno, ' ', u.txtNombres) "
                                                        + " , m.txtModulo"
                                                        + " , p.txtPerfil"
                                                        + " , c.txtConexion"
                                                        + " , g.txtParametro"
                                                        + " FROM Usuario u"
                                                        + " INNER JOIN u.usuarioPerfilList up"
                                                        + " INNER JOIN up.nidPerfil p"
                                                        + " INNER JOIN p.nidConexion c"
                                                        + " INNER JOIN p.nidModulo m, General g"
                                                        + " WHERE g.txtDominio = 'TIPO_USUARIO'"
                                                        + " AND u.tipUsuario = g.txtValor"
                                                        + " AND u.flgActivo = '" + Constante.ESTADO_ACTIVO + "'"
                                                        + " AND up.flgActivo = '" + Constante.ESTADO_ACTIVO + "'"
                                                        + " AND p.flgActivo = '" + Constante.ESTADO_ACTIVO + "'"
                                                        + " AND m.flgActivo = '" + Constante.ESTADO_ACTIVO + "'"
                                                        + " AND (u.id = ?1 OR ?1 IS NULL)"
//                                                        + " AND (UPPER(u.codUsuario) = ?1 OR ?1 IS NULL)"
//                                                        + " AND (UPPER(CONCAT(u.txtApellidoPaterno, u.txtApellidoMaterno, u.txtNombres)) LIKE ?2 OR ?2 IS NULL)"
                                                        + " AND (p.id = ?2 OR ?2 IS NULL)"
                                                        + " AND (c.id = ?3 OR ?3 IS NULL)"
                                                        + " ORDER BY c.id")
})
public class Conexion extends EntidadBase implements Serializable {

    private static final long serialVersionUID = 1L;
    
    private String txtConexion;
    private String txtServername;
    private String txtDatabase;
    private String txtLogid;
    private String txtLogpass;
    private String txtDbparm;
    private String txtUrl;
    private Integer numPuerto;
    private String txtSid;
    private Integer numTamanioPool;
    private String txtDrivername;
    private List<Perfil> perfilList;

    public Conexion() {
    }

    @Id
    @Basic(optional = false)
    @NotNull
    @SequenceGenerator(schema="SSA", name="SQ_CONEXION",sequenceName="SSA.SQ_CONEXION", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SQ_CONEXION")
    @Column(name = "NID_CONEXION", nullable = false, precision = 0, scale = -127)
    @Override
    public Long getId() {
        return super.getId();
    }

    @Column(name = "NID_CONTROL_ACCESO_WEB", precision = 0, scale = -127)
    @Override
    public Long getNIdCtrlAccesoWeb() {
        return super.getNIdCtrlAccesoWeb();
    }
    
    @Column(name = "TXT_CONEXION", nullable = true, length = 100)
    public String getTxtConexion() {
        return txtConexion;
    }

    public void setTxtConexion(String txtConexion) {
        this.txtConexion = txtConexion;
    }
    
    @Basic(optional = false)
    @Column(name = "TXT_SERVERNAME", nullable = false, length = 50)
    public String getTxtServername() {
        return txtServername;
    }

    public void setTxtServername(String txtServername) {
        this.txtServername = txtServername;
    }

    @Basic(optional = false)
    @Column(name = "TXT_DATABASE", nullable = false, length = 50)
    public String getTxtDatabase() {
        return txtDatabase;
    }

    public void setTxtDatabase(String txtDatabase) {
        this.txtDatabase = txtDatabase;
    }

    @Basic(optional = false)
    @Column(name = "TXT_LOGID", nullable = false, length = 50)
    public String getTxtLogid() {
        return txtLogid;
    }

    public void setTxtLogid(String txtLogid) {
        this.txtLogid = txtLogid;
    }

    @Basic(optional = false)
    @Column(name = "TXT_LOGPASS", nullable = false, length = 50)
    public String getTxtLogpass() {
        return txtLogpass;
    }

    public void setTxtLogpass(String txtLogpass) {
        this.txtLogpass = txtLogpass;
    }

    @Column(name = "TXT_DBPARM", length = 50)
    public String getTxtDbparm() {
        return txtDbparm;
    }

    public void setTxtDbparm(String txtDbparm) {
        this.txtDbparm = txtDbparm;
    }

    @Column(name = "TXT_URL", length = 250)
    public String getTxtUrl() {
        return txtUrl;
    }

    public void setTxtUrl(String txtUrl) {
        this.txtUrl = txtUrl;
    }

    @Column(name = "NUM_PUERTO")
    public Integer getNumPuerto() {
        return numPuerto;
    }

    public void setNumPuerto(Integer numPuerto) {
        this.numPuerto = numPuerto;
    }

    @Column(name = "TXT_SID", length = 50)
    public String getTxtSid() {
        return txtSid;
    }

    public void setTxtSid(String txtSid) {
        this.txtSid = txtSid;
    }

    @Column(name = "NUM_TAMANIO_POOL")
    public Integer getNumTamanioPool() {
        return numTamanioPool;
    }

    public void setNumTamanioPool(Integer numTamanioPool) {
        this.numTamanioPool = numTamanioPool;
    }

    @Column(name = "TXT_DRIVERNAME", length = 50)
    public String getTxtDrivername() {
        return txtDrivername;
    }

    public void setTxtDrivername(String txtDrivername) {
        this.txtDrivername = txtDrivername;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "nidConexion")
    public List<Perfil> getPerfilList() {
        return perfilList;
    }

    public void setPerfilList(List<Perfil> perfilList) {
        this.perfilList = perfilList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (getId() != null ? getId().hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Conexion)) {
            return false;
        }
        Conexion other = (Conexion) object;
        if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !this.getId().equals(other.getId()))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "pe.gob.foncodes.ssa.bs.seguridad.domain.Conexion[ getId()=" + getId() + " ]";
    }
}
